package com.bed.lovewithpatientbed.controller;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.common.bean.UserDetailBean;
import com.bed.lovewithpatientbed.common.params.UserSearchParams;
import com.bed.lovewithpatientbed.pojo.User;
import com.bed.lovewithpatientbed.service.IOrderService;
import com.bed.lovewithpatientbed.service.IUserService;
import com.bed.lovewithpatientbed.util.ErrorMsgUtil;
import com.bed.lovewithpatientbed.util.LayUiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuxiaoding
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description 用户Controller
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private IUserService userService;

    private IOrderService orderService;

    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setOrderService(IOrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * @title updateUserPhone
     * @description 修改用户手机号
     * @author xuxiaoding
     */
    @GetMapping("/updateUserPhone")
    @ResponseBody
    public Map<String, Object> updateUserPhone(User user) {
        Map<String, Object> responseMap = new HashMap<>();

        if (user == null) {
            // 如果 user 为空， 直接返回
            responseMap.put("result", false);
            responseMap.put("errorMsg", ErrorMsgUtil.getErrorMsgList(new String[]{"解析参数为空"}));
            return responseMap;
        }

        if (user.getUserId() == null || user.getUserPhone() == null || user.getUserId() == 0 || "".equals(user.getUserPhone())) {
            responseMap.put("result", false);
            responseMap.put("errorMsg", ErrorMsgUtil.getErrorMsgList(new String[]{"传递参数不能为空"}));
            return responseMap;
        }

        // 更新
        boolean updateResult = userService.updateUserByPrimaryKeySelective(user);
        responseMap.put("result", updateResult);

        return responseMap;
    }

    /**
     * @title updateUserPhone
     * @description 修改用户登录密码
     * @author xuxiaoding
     */
    @PostMapping("/updateUserPassword")
    @ResponseBody
    public Map<String, Object> updateUserPassword(User user) {
        Map<String, Object> responseMap = new HashMap<>();

        if (user == null) {
            // 如果 user 为空， 直接返回
            responseMap.put("result", false);
            responseMap.put("errorMsg", ErrorMsgUtil.getErrorMsgList(new String[]{"解析参数为空"}));
            return responseMap;
        }

        if (user.getUserId() == null || user.getUserPassword() == null || user.getUserId() == 0 || "".equals(user.getUserPassword())) {
            responseMap.put("result", false);
            responseMap.put("errorMsg", ErrorMsgUtil.getErrorMsgList(new String[]{"传递参数不能为空"}));
            return responseMap;
        }

        // 更新
        boolean updateResult = userService.updateUserByPrimaryKeySelective(user);
        responseMap.put("result", updateResult);

        return responseMap;
    }

    /**
     * @title deleteUser
     * @description 删除单个用户
     * @author xuxiaoding
     * @param userId 用户Id
     */
    @RequiresPermissions("user:delete:*")
    @PostMapping("/delete")
    @ResponseBody
    public String deleteUser(Integer userId) {
        if (userId == null || userId < 0) {
            return "param error";
        }

        boolean result = userService.softDeleteUser(userId);
        if (result) {
            return "success";
        } else {
            return "failed";
        }
    }

    /**
     * @title batchDeleteUser
     * @description 批量删除用户
     * @author xuxiaoding
     * @param userIdList 用户Idlist
     */
    @RequiresPermissions("user:delete:*")
    @PostMapping("/batchDelete")
    @ResponseBody
    public String batchDeleteUser(Integer[] userIdList) {
        if (userIdList == null || userIdList.length < 1) {
            return "param error";
        }

        boolean result = userService.batchSoftDeleteUser(userIdList);
        if (result) {
            return "success";
        } else {
            return "failed";
        }
    }

    /**
     * @title getUserByConditionWithLimit
     * @description 按条件分页查询用户信息
     * @author xuxiaoding
     * @param pageBean 分页信息
     * @param userSearchParams 查询条件
     * @return 返回
     */
    @RequiresPermissions("user:retrieve:*")
    @GetMapping("/getUser")
    @ResponseBody
    public Map<String, Object> getUserByConditionWithLimit(LayUiPageBean pageBean, UserSearchParams userSearchParams) {
        System.out.println("UserController.getUserByConditionWithLimit");
        System.out.println(pageBean);
        System.out.println(userSearchParams);
        // 按条件查询总数
        long count = userService.getCountByCondition(userSearchParams);
        // 分页按条件查询
        List<User> userList = userService.retrieveUserByConditionWithLimit(pageBean, userSearchParams);
        // LayUI 展示封装类
        List<UserDetailBean> userDetailBeanList = new ArrayList<>();
        /*
        封装UserDetailBean
         */
        if (userList != null) {
            for (User user : userList) {
                UserDetailBean userDetailBean = new UserDetailBean();
                userDetailBean.setUserId(user.getUserId());
                userDetailBean.setUserPhone(user.getUserPhone());
                userDetailBean.setUserNickname(user.getUserNickname());
                userDetailBean.setUserSex(user.getUserSex());
                userDetailBean.setUserTypeName(user.getUserType().getUserTypeName());
                userDetailBean.setUserRegisterDate(user.getUserRegisterDate());
                // 根据 userId 查询订单总数
                long orderTotalCount = orderService.getOrderCountByUserId(user.getUserId());
                userDetailBean.setOrderTotalCount(orderTotalCount);
                // 查询累计消费
                BigDecimal payTotal = orderService.getOrderTotalPayByUserId(user.getUserId());
                userDetailBean.setPayTotal(payTotal);

                userDetailBeanList.add(userDetailBean);
            }
        }

        return LayUiUtil.getLayTableResponseMap("success", 0, count, userDetailBeanList);
    }
}
